﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Text;
using System.Collections;


namespace Billder.BL
{
    public class BOM : Part
    {

        public string Rev;
        public float SalesQty;
        public float LOH;
        public float Profit;
        public string Desc;
        public List<Part> Parts;
        public string asJS() {
            return this.asJS(true);
        }
        public string asJS(bool child) {
            string varName = "__BOM" + Guid.NewGuid().ToString("N").Substring(16);
            StringBuilder sb = new StringBuilder();
            if (!child) {
                sb.Append("var ").Append(varName).Append(" = ");
            }
            sb.AppendFormat("new BOM({0} , '{1}' , {2} , ", this.ID, this.Name, this.Rev);
            sb.Append("{");
            sb.AppendFormat("SalesQty : {0} , ", this.SalesQty);
            sb.AppendFormat("LOH :{0},",  this.LOH);
            sb.AppendFormat("Profit :{0} , ", this.Profit);
            sb.AppendFormat("Desc :'{0}',", this.Desc);
            sb.AppendFormat("Qty : {0},", this.Qty);
            StringBuilder partsJS = new StringBuilder();
            bool first = true;
            foreach (BL.Part part in this.Parts)
            {
                if (part is BOM)
                {
                    partsJS.AppendFormat("{0} {1}", first ? "" : ",", ((BL.BOM)part).asJS(true));
                }
                else
                {
                    partsJS.AppendFormat("{0} {1}", first ? "" : ",", part.asJS());
                }
                first = false;
            }
            sb.AppendFormat("Parts : [{0}]", partsJS.ToString());
            sb.Append("})");
            if (!child) {
                sb.Append(";\n");
            }
            return sb.ToString();
        }
        public BOM(int ID ,string Name , int Qty , string Rev , float SalesQty , float LOH , float  Profit , string Desc ) : base( ID , Name, Qty)
        {
            this.Rev = Rev;
            this.SalesQty = SalesQty;
            this.LOH = LOH;
            this.Profit = Profit;
            this.Desc = Desc;
            Parts = new List<Part>();
        }
        public BOM(string XML) : base(XML)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml( XML );
            XmlNode bom = doc.GetElementsByTagName("bom")[0];
            this.Name = bom.Attributes["name"].Value;
            this.Rev = bom.Attributes["revision"].Value;
            this.SalesQty = float.Parse(bom.Attributes["salesqty"].Value);
            string strLOH = bom.Attributes["loh"].Value;
            strLOH = strLOH.Equals(string.Empty) ? "0" : strLOH;
            string strProfit = bom.Attributes["profit"].Value;
            strProfit = strProfit.Equals(string.Empty) ? "0" : strProfit;
            this.LOH = float.Parse(strLOH);
            this.Profit = float.Parse(strProfit);
            this.Desc = bom.SelectSingleNode("/bom/description").InnerText;
            Parts = new List<Part>();
            XmlNodeList parts = bom.SelectNodes("/bom/part");
            foreach (XmlNode part in parts) {
                if (part.SelectNodes("./component").Count == 1)
                {
                    Parts.Add(new Part(part.InnerXml));
                }
                else {
                    Parts.Add(new BOM(part.InnerXml));
                }
            }
        }
    }
}